#include "chunk.h"
#include "game.h"

Chunk::Chunk()
{
	this->x = 0;
	this->z = 0;
	
	for(int x=0;x<16;x++)
	{
		for(int z=0;z<16;z++)
		{
			for(int y=0;y<16;y++)
			{
				blocks[x][y][z] = Block::herbe;
			}
		}
	}
}

void Chunk::draw()
{
	glBindTexture(GL_TEXTURE_2D, Game::getInstance()->getTextureManager()->get("texture.bmp"));
	glBegin(GL_QUADS);
	for(int x=0;x<16;x++)
	{
		for(int z=0;z<16;z++)
		{
			for(int y=0;y<16;y++)
			{
				if(blocks[x][y][z] != NULL)
				{
					if(blocks[x][y][z]->doesRenderCubic())
					{
						for(int k=0; k<6; k++)
						{
							bool render = false;
							if(k==0 && (y==0 || blocks[x][y-1][z] == NULL || !blocks[x][y-1][z]->doesRenderCubic()))
							{
								render = true;
							}
							else if(k==1 && (y==15 || blocks[x][y+1][z] == NULL || !blocks[x][y+1][z]->doesRenderCubic()))
							{
								render = true;
							}
							else if(k==2 && (x==15 || blocks[x+1][y][z] == NULL || !blocks[x+1][y][z]->doesRenderCubic()))
							{
								render = true;
							}
							else if(k==3 && (z==15 || blocks[x][y][z+1] == NULL || !blocks[x][y][z+1]->doesRenderCubic()))
							{
								render = true;
							}
							else if(k==4 && (x==0 || blocks[x-1][y][z] == NULL || !blocks[x-1][y][z]->doesRenderCubic()))
							{
								render = true;
							}
							
							if(render)
							{
							blocks[x][y][z]->drawFace(k,x+this->x*16, y, z+this->z*16);
							}
						
						}
					}
					else
					{
						blocks[x][y][z]->draw(x+this->x*16, y, z+this->z*16);
					}
				}
			}
		}
	}
	glEnd();
	
}